﻿/*
 * Created by SharpDevelop.
 * User: Dhanesh
 * Date: 11-10-2008
 * Time: 13:16:20
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;

namespace ChallanSystem
{
	/// <summary>
	/// Description of Transporters.
	/// </summary>
	public partial class Transporters : Form
	{
		Transporter trans = null;
		DataSet ds = null;
		DataRow dr = null;
		DataTable dtFiltered = null;
		
		public Transporters()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		void BtnNewClick(object sender, EventArgs e)
		{
			ClearControls();
			enableAllControls(null, null);
			btnSave.Enabled = true;
			btnCancel.Enabled = true;
			btnNew.Enabled = false;
			btnUpdate.Enabled = false;
			btnDelete.Enabled = false;
		}
		
		void BtnSaveClick(object sender, EventArgs e)
		{
			if(validate())
			{
				if(isNumericCheck())
				{
					try
					{
						trans = new Transporter();
						trans.Name = txtTransporterName.Text;
						trans.Address1 = txtAdd1.Text != ""?txtAdd1.Text:" ";
						trans.Address2 = txtAdd2.Text != ""?txtAdd2.Text:" ";
						trans.Address3 = txtAdd3.Text != ""?txtAdd3.Text:" ";
						trans.Postcode = txtPinCode.Text != ""?txtPinCode.Text:" ";
						trans.Phone1 = txtPhone1.Text != ""?txtPhone1.Text:" ";
						trans.Phone2 = txtPhone2.Text != ""?txtPhone2.Text:" ";
						trans.Fax = txtFax.Text != ""?txtFax.Text:" ";
						trans.Email = txtEmail.Text != ""?txtEmail.Text:" ";
						
						bool ret = trans.Add();
						if(ret == true)
						{
							MessageBox.Show("Data added Successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
							btnSave.Enabled = false;
							btnCancel.Enabled = false;
							btnNew.Enabled = true;
							btnUpdate.Enabled = true;
							btnDelete.Enabled = true;
							ClearControls();
							RefreshDataGrid();
						}
					}
					catch(Exception expSave)
					{
						MessageBox.Show("Sorry... Cannot Process Your Request "+expSave.ToString());
					}
				}
				else
					MessageBox.Show("Please Enter Valid Data !!!");
			}
			else
			{
				MessageBox.Show("All (*) Fields Are Mandatory !!!");
			}
		}
		
		void BtnDeleteClick(object sender, EventArgs e)
		{
			if(dr != null)
			{
				int id = Convert.ToInt32(dr["Transporter_ID"].ToString());
				trans = new Transporter();
				
				bool ret = trans.Delete(id);
				if(ret == true)
				{
					MessageBox.Show("Data deleted successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
					btnSave.Enabled = false;
					btnCancel.Enabled = false;
					btnNew.Enabled = true;
					ClearControls();
					RefreshDataGrid();
				}
			}
			else
			{
				MessageBox.Show("No row selected to update.");
			}
		}
		
		void BtnCancelClick(object sender, EventArgs e)
		{
			ClearControls();
			disableAllControls(null, null);
			btnNew.Enabled = true;
			btnUpdate.Enabled = true;
			btnDelete.Enabled = true;
			btnSave.Enabled = false;
			btnCancel.Enabled = false;
			RefreshDataGrid();
		}
		
		void PopulateFields()
		{
			try
			{
				if(datagridTransporters.CurrentRowIndex > -1 && dtFiltered == null)
				{
					dr = ds.Tables[0].DefaultView[datagridTransporters.CurrentRowIndex].Row;
					txtTransporterName.Text = dr["Transporter_Name"].ToString();
					txtAdd1.Text = dr["Address1"].ToString();
					txtAdd2.Text = dr["Address2"].ToString();
					txtAdd3.Text = dr["Address3"].ToString();
					txtPinCode.Text = dr["Postcode"].ToString();
					txtPhone1.Text = dr["Phone1"].ToString();
					txtPhone2.Text = dr["Phone2"].ToString();
					txtFax.Text = dr["Fax"].ToString();
					txtEmail.Text = dr["Email"].ToString();
				}
				else
				{
					dr = dtFiltered.DefaultView[datagridTransporters.CurrentRowIndex].Row;
					txtTransporterName.Text = dr["Transporter_Name"].ToString();
					txtAdd1.Text = dr["Address1"].ToString();
					txtAdd2.Text = dr["Address2"].ToString();
					txtAdd3.Text = dr["Address3"].ToString();
					txtPinCode.Text = dr["Postcode"].ToString();
					txtPhone1.Text = dr["Phone1"].ToString();
					txtPhone2.Text = dr["Phone2"].ToString();
					txtFax.Text = dr["Fax"].ToString();
					txtEmail.Text = dr["Email"].ToString();
				}
			}
			catch(IndexOutOfRangeException)
			{
				
			}
		}
		
		void RefreshDataGrid()
		{
			trans = new Transporter();
			ds = trans.View();
			datagridTransporters.SetDataBinding(ds, "Transporters");
		}
		
		void ClearControls()
		{
			txtTransporterName.Text = String.Empty;
			txtAdd1.Text = String.Empty;
			txtAdd2.Text = String.Empty;
			txtAdd3.Text = String.Empty;
			txtPinCode.Text = String.Empty;
			txtPhone1.Text = String.Empty;
			txtPhone2.Text = String.Empty;
			txtFax.Text = String.Empty;
			txtEmail.Text = String.Empty;
		}
		
		void TransportersLoad(object sender, EventArgs e)
		{
			disableAllControls(null, null);
			RefreshDataGrid();
		}
		
		void DatagridTransportersClick(object sender, EventArgs e)
		{
			PopulateFields();
		}
		
		void DatagridTransportersCurrentCellChanged(object sender, EventArgs e)
		{
			PopulateFields();
		}
		
		void enableAllControls(object sender, EventArgs e)
		{
			txtAdd1.Enabled = true;
			txtAdd2.Enabled = true;
			txtAdd3.Enabled = true;
			txtEmail.Enabled = true;
			txtFax.Enabled = true;
			txtPhone1.Enabled = true;
			txtPhone2.Enabled = true;
			txtPinCode.Enabled = true;
			txtTransporterName.Enabled = true;
		}

		void disableAllControls(object sender, EventArgs e)
		{
			txtAdd1.Enabled = false;
			txtAdd2.Enabled = false;
			txtAdd3.Enabled = false;
			txtEmail.Enabled = false;
			txtFax.Enabled = false;
			txtPhone1.Enabled = false;
			txtPhone2.Enabled = false;
			txtPinCode.Enabled = false;
			txtTransporterName.Enabled = false;
		}

		void Button1Click(object sender, EventArgs e)
		{
			Util searchUtil = new Util();
			string filter = string.Empty;
			filter = txtSearch.Text;

			string columnName = string.Empty;
			columnName = "Transporter_Name";
			
			//MessageBox.Show("Filter B4 : "+filter);
//			filter = "Customer_Name = '"+ds.Tables[0].DefaultView[0].Row["Customer_Name"].ToString()+"'";
			//MessageBox.Show("Filter After : "+filter);
			dtFiltered = searchUtil.Search(ds.Tables[0], filter, columnName);
			datagridTransporters.DataSource = dtFiltered;
		}
		void BtnUpdateClick(object sender, EventArgs e)
		{
			try
			{
				
				if(dr != null)
				{
					int id = Convert.ToInt32(dr["Transporter_ID"].ToString());
					trans = new Transporter();
					trans.Name = txtTransporterName.Text;
					trans.Address1 = txtAdd1.Text != ""?txtAdd1.Text:" ";
					trans.Address2 = txtAdd2.Text != ""?txtAdd2.Text:" ";
					trans.Address3 = txtAdd3.Text != ""?txtAdd3.Text:" ";
					trans.Postcode = txtPinCode.Text != ""?txtPinCode.Text:" ";
					trans.Phone1 = txtPhone1.Text != ""?txtPhone1.Text:" ";
					trans.Phone2 = txtPhone2.Text != ""?txtPhone2.Text:" ";
					trans.Fax = txtFax.Text != ""?txtFax.Text:" ";
					trans.Email = txtEmail.Text != ""?txtEmail.Text:" ";
					
					bool ret = trans.Update(id);
					if(ret == true)
					{
						MessageBox.Show("Data updated successfully!","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
						btnSave.Enabled = false;
						btnCancel.Enabled = false;
						btnNew.Enabled = true;
						ClearControls();
						RefreshDataGrid();
					}
				}
				else
				{
					MessageBox.Show("No row selected to update.");
				}
			}
				catch(Exception expUpdate)
				{
					MessageBox.Show("Sorry... Cannot Process Your Request "+expUpdate.ToString());
				}
			}
			
			bool validate()
			{
				bool validate = true;
				
				if(txtPhone1.Text == "" || txtAdd1.Text == "" || txtPinCode.Text == "" || txtTransporterName.Text == "")
					validate = false;
				else
					validate = true;
				
				return validate;
			}
			bool isNumericCheck()
			{
				bool validate = true;
				
				try
				{
					int intValue = int.Parse(txtPhone1.Text);
					int intValue3 = int.Parse(txtPinCode.Text);
				}
				catch(Exception)
				{
					validate = false;
				}
				return validate;
			}
		}
	}
